package com.ruowei.modules.webSocket.pojo;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruowei.modules.webSocket.domain.Message;
import org.apache.commons.lang3.StringUtils;

import javax.websocket.DecodeException;
import javax.websocket.EndpointConfig;
import java.io.IOException;

public class MessageDecoder implements javax.websocket.Decoder.Text<Message> {
    @Override
    public Message decode(String msg) throws DecodeException {
        if (StringUtils.isEmpty(msg)) {
            return null;
        }
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.readValue(msg, Message.class);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public boolean willDecode(String s) {
        return true;
    }

    @Override
    public void init(EndpointConfig endpointConfig) {

    }

    @Override
    public void destroy() {

    }
}
